﻿@model SearchModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";

    //title
    Html.AddTitleParts(T("PageTitle.Search").Text);
    int startitem = Model.PagingFilteringContext.PageSize * (Model.PagingFilteringContext.PageNumber - 1) + 1;
    int enditem = Model.PagingFilteringContext.TotalItems;
    var isLogin = !EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsGuest();
    ViewBag.IsSearch = true;
}
@using Autofac
@using Nop.Core;
@using Nop.Core.Domain.Customers
@using Nop.Core.Infrastructure;
@using Nop.Web;
@using Nop.Web.Models.Catalog;
@using Nop.Web.Extensions;

@using (Html.BeginRouteForm("ProductSearch", FormMethod.Get, new { id = "productsearchform" }))
{
    //if (String.IsNullOrEmpty(Model.Q) && Model.Cid > 0 && Model.Products.Count > 0)
    //{
    //    Model.FeaturedProduct = Model.Products.Last();
    //}
    @Html.HiddenFor(model => model.Sc)
    @Html.HiddenFor(model => model.MinPrice)
    @Html.HiddenFor(model => model.MaxPrice)
    @Html.HiddenFor(model => model.Cid)
    <section class="search-container">
        <h2 class="page-title"><span class="title-image"></span> @((!String.IsNullOrEmpty(Model.CategoryName) && Model.Cid > 0) ? Model.CategoryName : (String.IsNullOrEmpty(Model.Q) && !(Model.Cid > 0)) ? "Browse Products" : T("Search") + " Results") @(!String.IsNullOrEmpty(Model.Q) ? "for \"" + Model.Q + "\"" : "")</h2>
        <ul class="breadcrumb search-breadcrumb">
            <li>
                <a href="@Url.RouteUrl("HomePage")">@T("HomePage")</a>
            </li>
            @if (!String.IsNullOrWhiteSpace(Model.Q))
            {
                <li>
                    <a href="@Url.RouteUrl("ProductSearch")">@T("Search")</a>
                </li>
            }
            else if (!(Model.Cid > 0))
            {
                <li class="current">Browse</li>
            }
            @if (!String.IsNullOrWhiteSpace(Model.Q))
            {
                if (Model.Cid > 0)
                {
                    <li>
                        @Html.ActionLink("\"" + Model.Q + "\"", "Search", new { Q = Model.Q })
                    </li>
                }
                else
                {
                    <li class="current">"@Model.Q"</li>
                }
            }
            @foreach (var cat in Model.ParentsCategory)
            {
                if (Model.Cid.ToString() != cat.Value)
                {
                    <li>
                        @Html.ActionLink(cat.Text, "Search", new { Q = Model.Q, Cid = cat.Value })
                    </li>
                }
                else
                {
                    <li class="current">@cat.Text</li>
                }
            }
        </ul>

        <div class="row">
            <aside class="search-filter col-xxs-12 col-xs-12 col-sm-4 col-md-3 col-lg-3">

                <div class="input-group search-field">
                    <span class="input-group-addon search-submit" id="search-icon"><i class="fa fa-search"></i></span>
                    @Html.TextBoxFor(model => model.Q, new { @class = "form-control", placeholder = T("Search.Placeholder"), aria_describedby = "search-icon" })
                </div>

                @if (Model.ShowCategoryFilter && (Model.AvailableCategories.Count > 0 || Model.ParentsCategory.Count > 0))
                {
                    <div class="filter-section">
                        <h3 class="filter-section-title">@T("Categories")</h3>
                        <div class="filter-section-body">

                            @if (Model.ParentsCategory.Count > 0)
                            {
                                <div class="parents-category">
                                    @{
                                var l = 0; // Parent level
                                var pid = ""; // Parent id
                                foreach (var p in Model.ParentsCategory)
                                {
                                    <div class="parents-category-item parent-@l" data-pid="@pid">
                                        <a href="@Url.RouteUrl("ProductSearch", new { Cid = p.Value, Q = Model.Q })">@p.Text</a> <i class="fa fa-times remove-category"></i>
                                        @if (l > 0)
                                        { <span class="joiner"></span> }
                                    </div>
                                        l++;
                                        pid = p.Value;
                                }
                                    }
                                </div>
                            }

                            @if (Model.AvailableCategories.Count > 0)
                            {
                                <div class="available-categories">
                                    <div>
                                        @if (Model.ParentsCategory.Count > 0)
                                        {
                                            <h4>
                                                @if (!String.IsNullOrEmpty(Model.Q))
                                                {<text>Refine Further</text>}
                                                else
                                                {<text>Subcategories</text>}
                                            </h4>
                                        }
                                        @foreach (var category in Model.AvailableCategories)
                                        {
                                            <div class="available-category" data-val="@category.Value">
                                                <a href="@Url.RouteUrl("ProductSearch", new { Cid = category.Value, Q = Model.Q })">@category.Text</a>
                                            </div>
                                        }
                                    </div>
                                </div>
                            }


                        </div>
                    </div>
                }
                <div class="search-results-sort filter-section">
                    <h3 class="filter-section-title">@T("Categories.OrderBy")</h3>
                    <div class="filter-section-body">
                        @Html.DropDownListFor(model => model.OrderBy, Model.AvailableSortmethods, new { @class = "form-control" })
                    </div>
                </div>
                @if (Model.ShowPriceFilter)
                {
                    <div class="filter-section">
                        <h3 class="filter-section-title">@T("Search.PriceRange")</h3>
                        <div class="filter-section-body">
                            @Html.HiddenFor(model => model.Pf, new { @class = "price-from", @readonly = "readonly" })
                            @Html.HiddenFor(model => model.Pt, new { @class = "price-to", @readonly = "readonly" })
                            <div class="price-slider-text">
                                @T("Search.PriceRange.From")
                                $
                                <span class="pricefrom"></span>
                                @T("Search.PriceRange.To")
                                $
                                <span class="priceto"></span>
                            </div>
                            <div class="price-slider-wrapper">
                                <div id="pricerange-slider"></div>
                            </div>
                        </div>
                    </div>
                }
                @*<div class="reset-button text-right">
                        <button class="btn btn-danger reset" type="reset">Reset</button>
                    </div>*@
            </aside>

            <section class="search-results-wrapper col-xxs-12 col-xs-12 col-sm-12 col-md-9 col-lg-9">

                @if (Model.PagingFilteringContext.TotalItems > 0)
                {
                    @*<div class="search-filter-toggle-bar"><span>Showing results @startitem - @enditem of @Model.PagingFilteringContext.TotalItems</span><span class="search-filter-toggle"></span></div>*@
                }

                @if (Model.Products.Count < 1)
                {
                    <div class="no-results">

                        @if (!String.IsNullOrEmpty(Model.Warning))
                        {
                            <span class="result">@Model.Warning</span>
                        }
                        else
                        {
                            <span class="result">@T("Search.NoResultsText")</span>
                        }

                    </div>

                }
                else
                {
                    <div class="row">
                        @if (Model.FeaturedProduct != null)
                        {
                            @Html.Partial("_FeaturedProductBox", Model.FeaturedProduct)
                        }
                        @{
                        var viewDataDict = new ViewDataDictionary();
                        viewDataDict.Add("HasSide", true);
                        viewDataDict.Add("IsLogin", isLogin);
                        
                        foreach (var product in Model.Products)
                        {
                            @Html.Partial("_ProductBox", product, viewDataDict)
                        }
                        }
                    </div>
                }

                @*</div>*@
                <div class="clear"></div>
                @if (Model.PagingFilteringContext.TotalPages > 1)
                {
                    <div class="pager">
                        @Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber").BooleanParameterName("as").BooleanParameterName("isc").BooleanParameterName("sid")
                    </div>
                }
                <div class="search-results-summary col-md-12">

                    @{

    if (Model.PagingFilteringContext.PageNumber != Model.PagingFilteringContext.TotalPages)
    {
        enditem = Model.PagingFilteringContext.PageSize * Model.PagingFilteringContext.PageNumber;
    }
                    }
                    @if (Model.PagingFilteringContext.TotalItems > 0)
                    {
                        @*<div>Showing results @startitem - @enditem of @Model.PagingFilteringContext.TotalItems</div>*@
                    }
                    <div class="back-to-top">
                        <a href="#backtop"><i class="fa fa-angle-up"></i></a>
                    </div>
                </div>
            </section>
            <div class="clear"></div>
        </div>
    </section>

    if (Model.Products.Count < 1)
    {
        @*<div class="clear"></div>
            @Html.Action("HomepageBestSellers", "Catalog")*@
    }
    @section Bottom
    {
        @*@Html.Action("RecentlyViewedProductsBlock", "Catalog")*@
        <div class="clear"></div>
    }
}
<script type="text/javascript">
    $(".category-filter .selected-text").click(function () {
        if ($(".category-filter-list").height() == 0) {
            $(".category-filter-list").stop().animate({ "height": $(".category-filter-list-wrapper").outerHeight() }, 150);
        } else {
            $(".category-filter-list").stop().animate({ "height": 0 }, 150);
        }
    });

    $(".category-filter-list-item").click(function () {
        $(".category-filter-list-item").removeClass("selected")
        if ($(this).attr("data-cid") > 0) {
            $(this).addClass("selected");
            //$(".category-filter-text .selected-text").text($('.search-category-column option:selected').text());
            $(".category-filter-text .selected-text").text($('.category-filter-list-item.selected').text());
            $(".remove-filter-category").text("Remove");
        } else {
            $(".category-filter-text .selected-text").text("Select a Category");
            $(".remove-filter-category").text("");
        }
        $('.search-category-column select, .category-filter #Cid').val($(this).attr("data-cid"));
        $(".search-category-column [data-val=" + $(this).attr("data-cid") + "]").trigger("click");
        $(".category-filter-list").stop().animate({ "height": 0 }, 150);
        $(".button-grey.search-button").trigger('click');
    });
    $(".filter-category-menu .available-category").click(function () {
        $("input#Cid").remove();
        $(this).closest("form").append("<input type='hidden' name='Cid' id='Cid\' value='" + $(this).attr("data-val") + "' />");
        $(this).closest("form").submit();
    });
    $(".remove-category").click(function () {
        $("input#Cid").val($(this).closest(".parents-category-item").attr("data-pid"));
        $(this).closest("form").submit();
    });
    $(".reset").click(function () {
        $("input#Cid").val($(this).closest(".parents-category-item").attr("data-pid"));
        $(this).closest("form").submit();
    });
    $("#OrderBy").change(function () {
        $(this).closest("form").submit();
    });
    var minrange = parseInt("@Model.MinPrice");
    var maxrange = parseInt("@Model.MaxPrice") + 1;
    var minval = parseInt("@Model.Pf");
    var maxval = parseInt("@Model.Pt");
    if (minrange == "" || isNaN(minrange)) {
        minrange = 0;
    }
    if (maxrange == "" || isNaN(maxrange)) {
        maxrange = 1000;
    }
    if (minval == "" || isNaN(minval)) {
        minval = minrange;
    }
    if (maxval == "" || isNaN(maxval)) {
        maxval = maxrange;
    }
    $("#Pf").val(minval);
    $("#Pt").val(maxval);
    $(".pricefrom").text(minval);
    $(".priceto").text(maxval);
    $("#pricerange-slider").slider({
        range: true,
        min: minrange,
        max: maxrange,
        values: [minval, maxval],
        slide: function (event, ui) {
            $("#Pf").val(ui.values[0]);
            $("#Pt").val(ui.values[1]);
            $(".pricefrom").text(ui.values[0]);
            $(".priceto").text(ui.values[1]);
        },
        stop: function () {
            $(this).closest("form").submit();
        }
    });

    $('.search-category-column select').change(function () {
        if ($('.search-category-column select').val() > 0) {
            $(".category-filter-text .selected-text").text($('.search-category-column option:selected').text());
            $(".remove-filter-category").text("Remove");
        } else {
            $(".category-filter-text .selected-text").text("Select a Category");
            $(".remove-filter-category").text("");
        }
    });

    $(".remove-filter-store").click(function () {
        $(this).text("");
        $(".store-logo").hide();
        $("#Store_Id").val(0);
    });

    $(".remove-filter-searchtext").click(function () {
        $(".filter-searchtext input#Q").val("");
        $("#small-searchterms").val("");
        $(this).text("");
    });

    $(".remove-filter-pricerange").click(function () {
        $("#Pf, #Pt").val("");
        $(this).text("");
    });

    $(".search-field input#Q").keypress(function (e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if (keycode == '13') {
            $(this).closest("form").submit();
        }
        $("#small-searchterms").val($(this).val());
    });
    $(".search-field .search-submit").click(function () {
        $(this).closest("form").submit();
    });
    $("#small-searchterms").change(function () {
        $(".filter-searchtext input#Q").val($("#small-searchterms").val());
        if ($(this).val() != "") {
            $(".remove-filter-searchtext").text("Clear");
        } else {
            $(".remove-filter-searchtext").text("");
        }
    });

    $("#Pf, #Pt").change(function () {
        if ($("#Pf").val() != "" || $("#Pt").val() != "") {
            $(".remove-filter-pricerange").text("Clear");
        } else {
            $(".remove-filter-pricerange").text("");
        }
    });
    $(document).ready(function () {
        $(".search-text").val($("#small-searchterms").val());
    });

    $('.collapse').collapse({
        toggle: true
    });

    $(window).scroll(function () {
        if ($(this).scrollTop()) {
            $('.back-to-top').fadeIn();
        } else {
            $('.back-to-top').fadeOut();
        }
    });

    $(".back-to-top").click(function (e) {
        e.preventDefault();
        $("body,html").animate({ scrollTop: 0 }, 500);
    });

    //Scrollwheel click disable
    $(".filter-category-menu").on('click', function (e) {
        if (e.which == 2) {
            e.preventDefault();
        }
    });
</script>